<?php
/**
 * Created by Command.
 * User: Vito
 */

declare (strict_types = 1);

namespace app\model;
use app\model\traits\TreeTrait;

class PayRecord extends BaseModel
{

    const STATUS_DISABLE = 0;
    const SEX_START = 1;

    protected $statusEnum = [
        self::STATUS_DISABLE => '待缴费',
        self::SEX_START      => '已缴费',
    ];


    // use TreeTrait;
    // protected $name = 'PayRecord';
    /**
     * @Description: 关联房间表
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-10-29 11:31:50
     * @version: 1.0.0
     * @return [type]
     */
    public function room(){
        return $this->belongsTo('room','room_id')->with('building')->bind(
            [
                'room_name'=>'name'
            ]
        );
    }
    /**
     * @Description: 远程一对一关联楼房表
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-10-29 11:35:36
     * @version: 1.0.0
     * @return [type]
     */
    public function building(){
        return $this->hasOneThrough('building', 'room','id','id','room_id','building_number');
    }

    /**
     * @Description: 关联用过户表
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-10-29 14:55:00
     * @version: 1.0.0
     * @return [type]
     */
    public function User(){

        return $this->belongsTo('user','user_id')->bind([
            'room_name'=>'real_name'
        ]);
    }

    /**
     * @Description: 获取子目录
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-10-31 09:31:22
     * @version: 1.0.0
     * @return [type]
     */    
    public function children()
    {
        return $this->hasMany($this->getName(), $this->parentKey ?? 'pid', $this->getPk())->with(['User']);
        // return $this->children('User', 'room', 'building');
    }
    
    /**
     * @Description: 获取缴费记录
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-10-31 09:32:16
     * @version: 1.0.0
     * @return [type]
     */    
    public  function getPayRecord($where){

        return self::where($where)->order('fee_date','desc')->find();
    }
    /**
     * @Description: 生成租户待’缴费记录
     * @author: xiayu <cb1071403016@126.com>
     * @param [type] $data
     * @Date: 2022-10-31 11:01:08
     * @version: 1.0.0
     * @return [type]
     */
    public static function addLesseeRecord($data){
        return self::create($data);
    }

    /**
     * @Description: 获取总价格
     * @author: xiayu <cb1071403016@126.com>
     * @param [type] $where
     * @Date: 2022-11-01 17:09:54
     * @version: 1.0.0
     * @return [type]
     */    
    public static function getTotal($where){

        return self::
        alias('PR')
        ->Where($where)
        ->join('wuye_room Room', 'Room.id = PR.room_id', 'right')
        ->sum('PR.fee');
    }

    /**
     * @Description: 获取记录详情
     * @author: xiayu <cb1071403016@126.com>
     * @param [type] $where
     * @Date: 2022-11-09 21:51:17
     * @version: 1.0.0
     * @return [type]
     */    
    public static function getRecordDetail($where){

        return self::withoutGlobalScope(['pid'])->where($where)->with(['room', 'User'])->order('fee_date', 'desc')->find();
    }

    /**
     * @Description: 获取用户全部缴费记录
     * @author: xiayu <cb1071403016@126.com>
     * @param [type] $where
     * @Date: 2022-11-10 15:30:24
     * @version: 1.0.0
     * @return [type]
     */    
    public static function getRecordList($where){

        return self::withoutGlobalScope(['pid'])->where($where)->with(['room', 'User'])->order('fee_date', 'desc')->select();

    }

    // // 查询后
    // public static function onAfterRead($model)
    // {
    //     foreach ($model as $key => $value) {
    //         # code...
    //         print_r($key);
            
    //     }
    //     die;
    // }

    /**
     * 全局查询范围
     * @var string[]
     */
    protected $globalScope = ['pid'];
    public function scopePid($query)
    {
         $query->where('pid', 0);
        
    }
}
